package com.sgcc.fj.pmos.e_fj_personalcenter.fragment;

import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sgcc.fj.pmos.arouter.personalcenter.PersonalCenterArouterPath;
import com.sgcc.fj.pmos.common.base.MvpBaseRecycleFragment;
import com.sgcc.fj.pmos.common.bean.BaseResponse;
import com.sgcc.fj.pmos.common.bean.EventCenter;
import com.sgcc.fj.pmos.common.dialog.CommonDialog;
import com.sgcc.fj.pmos.common.utils.ArguConstant;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.e_fj_personalcenter.adapter.DeviationCurveAdapter;
import com.sgcc.fj.pmos.e_fj_personalcenter.bean.PowerCurveBean;
import com.sgcc.fj.pmos.e_fj_personalcenter.contract.DeviationCurveContract;
import com.sgcc.fj.pmos.e_fj_personalcenter.presenter.DeviationCurvePresenter;


public class DeviationCurveFragment extends MvpBaseRecycleFragment<PowerCurveBean.DataBean, DeviationCurvePresenter> implements DeviationCurveContract.View {

    private DeviationCurveAdapter mDeviationCurveAdapter;
    private TextView mBtnAddCurve;

    @Override
    protected DeviationCurvePresenter createPresenter() {
        return new DeviationCurvePresenter();
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        mDeviationCurveAdapter = new DeviationCurveAdapter(mContext);
        View footer_view = getLayoutInflater().inflate(R.layout.footer_deviationcurve_layout, recycleview, false);
        findViewByIds(footer_view);
        mDeviationCurveAdapter.addFooterView(footer_view);
        mDeviationCurveAdapter.setItemBtnClickListener(new DeviationCurveAdapter.ItemBtnClickListener() {
            @Override
            public void onBtnDeleteClick(int position, PowerCurveBean.DataBean bean) {
                showDeleteDialog(bean.getRatioId());
            }

            @Override
            public void onBtnEditClick(int position, PowerCurveBean.DataBean bean) {
                ARouter.getInstance()
                        .build(PersonalCenterArouterPath.PERSONALCENTER_NEWCURVE)
                        .withString("from", "2")
                        .withSerializable("mBean", bean)
                        .navigation();
            }

            @Override
            public void onBtnRatioClick(int position, PowerCurveBean.DataBean bean) {
                ARouter.getInstance()
                        .build(PersonalCenterArouterPath.PERSONALCENTER_SETPOWERRATIO)
                        .withString("ratioId", bean.getRatioId())
                        .withString("from", "2")
                        .withBoolean("isEdit", true)
                        .navigation();
            }
        });
        return mDeviationCurveAdapter;
    }

    @Override
    public boolean isBindEventBusHere() {
        return true;
    }

    @Override
    public void onEventCallBack(EventCenter eventCenter) {
        super.onEventCallBack(eventCenter);

        super.onEventCallBack(eventCenter);
        switch (eventCenter.getEventCode()) {
            case ArguConstant.SAVE_CURVE_BACK:
                refresh(false);
                break;
            default:
                break;
        }
    }

    private void showDeleteDialog(String ratioId) {
        CommonDialog commonDialog = new CommonDialog(mContext);
        commonDialog.setTitle("提示");
        commonDialog.setMessage("确定要删除该曲线吗？");
        commonDialog.setOnClickBottomListener(new CommonDialog.OnClickBottomListener() {
            @Override
            public void onPositiveClick() {
                commonDialog.dismiss();
                mPresenter.deleteCurveInfo(ratioId);
            }

            @Override
            public void onNegtiveClick() {
                commonDialog.dismiss();
            }
        });
        commonDialog.show();
    }

    private void findViewByIds(View footer_view) {
        mBtnAddCurve = (TextView) footer_view.findViewById(R.id.btn_add_curve);
        mBtnAddCurve.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance()
                        .build(PersonalCenterArouterPath.PERSONALCENTER_NEWCURVE)
                        .withString("from", "2")
                        .navigation();
            }
        });
    }

    @Override
    public boolean isShowContent() {
        return true;
    }

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);
        mPresenter.queryCurveInfo();
    }

    @Override
    public void queryCurveInfoSuccess(PowerCurveBean response) {
        if (response != null) {
            if ("200".equals(response.getCode())) {
                loadSuccess(false, response.getData());
            }
        }
    }

    @Override
    public void deleteCurveInfoSuccess(BaseResponse response) {
        if (response != null) {
            if (response.isSuccess()) {
                refresh(true);
            }
        }
    }
}
